package com.study.commen.service

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
    private lateinit var mRetrofit: Retrofit
    private var mLogging: HttpLoggingInterceptor? = null
    private var mOkHttpBuilder: OkHttpClient.Builder? = null
    fun get(): Retrofit {
        mOkHttpBuilder =
            OkHttpClient.Builder().addInterceptor { chain ->
                val builder = chain.request().newBuilder()
                builder.addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                val request = builder.build()
                chain.proceed(request)
            }
        mLogging = HttpLoggingInterceptor()
        mLogging!!.level = HttpLoggingInterceptor.Level.BODY //设置查看日志的等级
        mOkHttpBuilder?.interceptors()?.add(mLogging) // 这句话是重点
        mRetrofit =
            Retrofit.Builder().client(mOkHttpBuilder?.build()).baseUrl("https://www.wanandroid.com")
                .addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(
                    RxJava2CallAdapterFactory.create())
                .build()
        return mRetrofit
    }
}